<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Index extends Controller{
    private $data_all = array();
    public function index(){
        $px = input('px')?input('px'):0;
        switch ($px) {
            case 0:
                $order = 'id desc';
                break;
            case 1:
                $order = 'filectime desc';
                break;
            case 2:
                $order = 'filemtime desc';
                break;
            case 3:
                $order = 'addtime desc';
                break;
            
            default:
                $order = 'id desc';
                break;
        }
        $list = Db::name('php_files')->order($order)->select();
        $this->assign('list', $list);
        return $this->fetch('Index/index');
    }
    function get_files(){
        $files = $this->my_dir("./");
        // dump($this->data_all);
        $data_all = $this->data_all;
        foreach ($data_all as $key => $value) {
            $map = array();
            $map['title'] = $value['title'];
            $arr = Db::name('php_files')->where($map)->find();
            $data = $value;
            if (empty($arr)) {
                $data['addtime'] = time();
                Db::name('php_files')->strict(false)->insert($data);
            }else{
                Db::name('php_files')->where('id='.$arr['id'])->update($data);
            }
        }
        $this->success('执行完成');
    }

    function view(){
        $arr = Db::name('php_files')->where('id='.input('id'))->find();
        if (empty($arr)) {
            echo "empty";
            die();
        }

        $str = '';
        if (file_exists($arr['title'])) {
           $file_arr = file($arr['title']);
            for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
                $str .= $file_arr[$i];
            }
        }
        $this->assign('str', $str);
        return $this->fetch('Index/view');
    }


    private function my_dir($dir) {
        $t = $_REQUEST['t']?$_REQUEST['t']:0;
        $files = array();
        if(@$handle = opendir($dir)) { //注意这里要加一个@，不然会有warning错误提示：）
            while(($file = readdir($handle)) !== false) {
                if($file != ".." && $file != ".") { //排除根目录；
                    if(is_dir($dir."/".$file)) { //如果是子文件夹，就进行递归
                        $files[$file] = $this->my_dir($dir."/".$file);
                    } else { //不然就将文件的名字存入数组；
                        if (strpos($file,'.php') !== false) {
                            $filemtime = filemtime($dir."/".$file);
                            $data = array(
                                    'title' => $dir."/".$file,
                                    'filesize' => filesize($dir."/".$file),
                                    'filemtime' => $filemtime,
                                    'filectime' => filectime($dir."/".$file)
                                );
                            $data['title'] = str_replace('.//', './', $data['title']);
                            $this->data_all[] = $data;
                            // if($t <= $filemtime) echo $dir."/".$file."<br>";
                        }
                        $files[] = $file;
                    }
                }
            }
            closedir($handle);
            return $files;
        }

    }
}